<?php
/**
 * 示例控制器
 * @version 1.0
 * @author: HuangYu <992287102@qq.com>
 * @since: 2019/4/2 15:30
 * Class DemoController
 * @package app\index\controller\demo
 */

namespace app\index\controller\demo;

use app\index\service\DemoService;

class DemoController extends CommonController
{
    private $demoService;

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();

        //初始化服务类
        $this->demoService = new DemoService();
    }

    /**
     * 示例列表
     * @author HuangYu
     * @since 2019/4/2
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function demoList()
    {
        //参数获取
        $param = [
            'keyword'    => $this->request->param('keyword'),
            'start_time' => $this->request->param('start_time'),
            'end_time'   => $this->request->param('end_time'),
            'page'       => $this->page,
            'page_size'  => $this->pageSize,
            'sort'       => $this->getSort(['create_time', 'demo_id' => 'id']), //排序
        ];
        //数据过滤
        $param = $this->arrayFilter($param);
        //数据处理
        $result = $this->demoService->demoList($param);
        //数据返回
        $data['list'] = [];
        foreach ($result['list'] as $item) {
            $data['list'][] = [
                'demo_id'     => $item['id'],
                'title'       => $item['title'],
                'remark'      => $item['remark'],
                'create_time' => strtotime($item['create_time']),
                'update_time' => $item['update_time'],
                'demo_one'    => isset($item['demo_one']) ? $item['demo_one'] : [],
                'demo_many'   => isset($item['demo_many']) ? $item['demo_many'] : [],
            ];
        }
        //分页信息
        $data['pagination'] = $this->pagination($result['count']);

        return $this->returnJson('000', '成功', $data);
    }

    /**
     * 示例详情
     * @author HuangYu
     * @since 2019/4/2
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function demoDetail()
    {
        //参数获取
        $param = [
            'demo_id' => $this->request->param('demo_id'),
        ];
        //数据过滤
        $param = $this->arrayFilter($param);
        //数据处理
        $item = $this->demoService->demoDetail($param);
        //数据返回
        if (!$item) {
            return $this->returnJson('500', '数据不存在');
        }
        $data = [
            'demo_id'     => $item['id'],
            'title'       => $item['title'],
            'remark'      => $item['remark'],
            'create_time' => strtotime($item['create_time']),
            'update_time' => $item['update_time'],
            'demo_one'    => isset($item['demo_one']) ? $item['demo_one'] : [],
            'demo_many'   => isset($item['demo_many']) ? $item['demo_many'] : [],
        ];

        return $this->returnJson('000', '成功', $data);
    }

    /**
     * 示例添加
     * @author HuangYu
     * @since 2019/4/2
     * @return \think\response\Json
     */
    public function demoAdd()
    {
        //参数获取
        $param = [
            'title'  => $this->request->param('title'),
            'remark' => $this->request->param('remark'),
        ];
        //数据过滤
        $param = $this->arrayFilter($param, ['remark']);
        //数据验证
        $msg = $this->validator('Demo', 'add', $param);
        if ($msg) {
            return $this->returnJson('500', $msg);
        }
        //数据处理
        $result = $this->demoService->demoAdd($param);
        //数据返回
        if ($result === false) {
            return $this->returnJson('500', '操作失败');
        } else {
            $data = [
                'demo_id' => (int)$result
            ];
            return $this->returnJson('000', '操作成功', $data);
        }
    }

    /**
     * 示例编辑
     * @author HuangYu
     * @since 2019/4/2
     * @return \think\response\Json
     */
    public function demoUpdate()
    {
        //参数获取
        $param = [
            'demo_id' => $this->request->param('demo_id'),
            'title'   => $this->request->param('title'),
            'remark'  => $this->request->param('remark'),
        ];
        //数据过滤
        $param = $this->arrayFilter($param, ['remark']);
        //数据验证
        $msg = $this->validator('Demo', 'update', $param);
        if ($msg) {
            return $this->returnJson('500', $msg);
        }
        //数据处理
        $result = $this->demoService->demoUpdate($param);
        //数据返回
        if ($result === false) {
            return $this->returnJson('500', '操作失败');
        } else {
            return $this->returnJson('000', '操作成功');
        }
    }

    /**
     * 示例删除
     * @author HuangYu
     * @since 2019/4/2
     * @return \think\response\Json
     */
    public function demoDelete()
    {
        //参数获取
        $param = [
            'demo_id'   => $this->request->param('demo_id'),
            'is_delete' => 1,
        ];
        //数据验证
        $msg = $this->validator('Demo', 'delete', $param);
        if ($msg) {
            return $this->returnJson('500', $msg);
        }
        //数据处理
        $result = $this->demoService->demoUpdate($param);
        //数据返回
        if ($result === false) {
            return $this->returnJson('500', '操作失败');
        } else {
            return $this->returnJson('000', '操作成功');
        }
    }
}